<template>
  <el-container class="mainContent">
    <el-row>
      <el-col class="title">家庭医生团队列表</el-col>
    </el-row>
    <el-row class="search-layer">
      <el-col>
        <el-form :inline="true" :model="formSearch" class="demo-form-inline">
          <el-form-item label="签约团队:">
            <el-input v-model="formSearch.groupName" placeholder="请输入团队名称"></el-input>
          </el-form-item>
          <el-form-item label="负责医生:">
            <el-input v-model="formSearch.doctor" placeholder="请输入负责医生名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchGroup">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="groupData" stripe style="width: 100%">
        <el-table-column prop="groupName" label="团队名称" width="300"></el-table-column>
        <el-table-column prop="teamItems" label="负责医生" width="300">
          <template slot-scope="scope">
            <span>{{ getDoctorName(scope.row.teamItems) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="teamItems" label="团队成员" width="400">
          <template slot-scope="scope">
            <span
              v-for="(item,index) in getTeamNames(scope.row.teamItems)"
              :key="index"
            >{{item}}&nbsp;</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
        class="pagination"
      ></el-pagination>
    </el-row>
  </el-container>
</template>
<script>
export default {
  name: "TeamManagement",
  data() {
    return {
      formSearch: {
        groupName: "",
        doctor: "",
      },
      groupData: [],
      currentPage: 1,
      count: 0,
      num: 10,
    };
  },

  methods: {
    getData() {
      this.axios({
        url: "/getTeamData",
        params: {
          page: this.currentPage,
          num: this.num,
          groupName: this.formSearch.groupName,
          doctor: this.formSearch.doctor,
        },
        methods: "get",
      })
        .then((result) => {
          //  console.log(result);
          if (result.data.code === 200) {
            this.groupData = result.data.data;
            this.count = result.data.count;
          } else {
            this.$message.error("请求数据失败");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getDoctorName(arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].manager === 1) {
          return arr[i].d_name;
        }
      }
    },
    getTeamNames(arr) {
      let newArr = [];
      for (let i = 0; i < arr.length; i++) {
        newArr.push(arr[i].d_name);
      }
      return newArr;
    },
    searchGroup() {
      this.getData();
    },
    add() {
      this.$router.push("/home/team/addTeamManagement")
    },
    handleEdit(index, row) {
      console.log(index, row);
      sessionStorage.setItem("id",row.tid)
      this.$router.push("/home/team/editTeamManagement")
    },
    handleDelete(index, row) {
      //   console.log(index, row);
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios({
            url: "/deleteTeam",
            data: { tid: row.tid },
            method: "post",
          })
            .then((res) => {
              //   console.log(res);
              if (res.data.code === 200) {
                this.getData();
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
              } else {
                this.$message({
                  showClose: true,
                  message: "删除失败",
                  type: "error",
                });
              }
            })
            .catch((err) => {
              this.$message({
                showClose: true,
                message: "删除失败",
                type: "error",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.num = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
      //   console.log(`当前页: ${val}`);
    },
  },
  mounted() {
    this.getData();
  },
};
</script>
<style lang="less" scoped>
.form-inline {
  width: 100%;
  height: 300px;
  text-align: left;
}
.mainContent {
  width: 100%;
  flex-direction: column;
  text-align: left;
}
.search-layer {
  width: 100%;
}
.pagination {
  margin-top: 50px;
  text-align: center;
}
.title {
  margin-bottom: 30px;
  font-size: 20px;
  font-weight: 600;
}
</style>